// JavaScript Document
var MAX_MARKERS = 1;
var totalMarkers = 0;
var map;
var actualMarker = null;
var markerLabel = null;
var actualRadius = null;
var registeredLocationList = new Array();
var locationRadiusList = new Array();

function initialize(){
	var latlng = new google.maps.LatLng(20.97490,-89.61977);
	var myOptions = {
		zoom : 10,
		center : latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
	google.maps.event.addListener(map,'click',function(event){
		totalMarkers++;
		placeMarker(event.latLng);
	});
}

function addLocationRadiusToList(lat,lng,inRadius,pos){
	var location = new google.maps.LatLng(lat,lng);
	var circle = new google.maps.Circle({center:location,map:null,radius:inRadius});
	locationRadiusList[pos] = circle;
}

function showRadiusFromList(position){
	var radius = locationRadiusList[position];
	radius.setMap(map);
}

function hideRadiusFromList(position){
	var radius = locationRadiusList[position];
	radius.setMap(null);
}

function hideLocationFromList(position){
	var rgsteredLocation = registeredLocationList[position];
	rgsteredLocation.setMap(null);
}

function showLocationFromList(position){
	var rgstredLocation = registeredLocationList[position];
	rgstredLocation.setMap(map);
}

function addLocationToList(latitude,longitude,position){
	var location = new google.maps.LatLng(latitude,longitude);
	var registeredMarker = new google.maps.Marker({position:location,map:null});
	registeredLocationList[position] = registeredMarker;
}

function placeMarker(location){
	if(actualMarker != null){
		actualMarker.setMap(null);
		//actualMarker = null;
		markerLabel.setMap(null);
		//actualMarker = null;
		if(actualRadius != null){
			actualRadius.setMap(null);
		}
	}
	actualMarker = new google.maps.Marker({
		position: location,
		map: map
	});
	markerLabel = new Label({map:map});
	markerLabel.bindTo('position',actualMarker,'position');
	markerLabel.set('zIndex',1234);
	changeMarkerTitle();
	var locationForm = document.RegisterLocationForm;
	locationForm.txt_LocationLat.value = location.lat();
	locationForm.txt_LocationLong.value = location.lng();
	toggleShowMapRadius();
}

function notShowMapRadius(){
	if(actualRadius != null){
		actualRadius.setMap(null);
	}
}

function showMapRadius(radiusSize){
	var circleOptions = {
		map:map,
		radius:parseInt(radiusSize,10),
		center:actualMarker.getPosition(),
		fillColor:"#00bfff"
	};
	if(actualRadius != null){
		actualRadius.setMap(null);
	}
	actualRadius = new google.maps.Circle(circleOptions);
}

function changeMarkerTitle(){
	var markerName = document.RegisterLocationForm.txt_LocationName;
	if(actualMarker != null){
		markerLabel.set('text',markerName.value);
	}
}